<?php

/*
* unit Controller class
* PHP versions 5.1.4
* @filesource
* @author   Manisha/Reetu
* @version 0.0.1 
*   - Initial release 
*/ 
class ServiceUnitsController extends AppController {
    /**
    * @Method      :beforeFilter
    * @Description : Parent Before filter calling
    * @access      :public
    * @param      :null
    * @return     :null
    */
    public function beforeFilter(){
        $this->layout = 'home';
        parent::beforeFilter();
    }
    
    /**
    * @Method       :index
    * @Description  :to set up the Lisitng of Service Units
    * @access       :public
    * @param        :null
    * @return       :null
    */
    public function index() {
        $userId = $this->Auth->User("id");
        $userType = $this->Auth->User('user_type');

        $savecrit = '';

        //criteria array for search
        $crit = array();
        $crit[] = "";

        if (isset($this->data)) {

            if (isset($this->data['ServiceUnit']['search_criteria']) && $this->data['ServiceUnit']['search_criteria'] != '') {
                $searchCriteriaTerm = trim($this->data['ServiceUnit']['search_criteria']);
                //will search with name and abbreviation
                $crit[] = "(ServiceUnit.unit_name like '%" . $searchCriteriaTerm . "%')";
                $savecrit .= "search_criteria:" . $searchCriteriaTerm . "/";
            }
        }
        //for passing the values of search conditions in params of paging to next page
        if (count($this->params['named']) > 0) {
            foreach ($this->params['named'] as $paramKey => $paramValue) {
                $savecrit .= $paramKey . ":" . $paramValue . "/";
            }
            if (isset($this->params['named']['search_criteria']))
                $crit[] = "(ServiceUnit.unit_name like '%" . $this->params['named']['search_criteria'] . "%')";
        }//if
        if (count($crit) > 0) {
            $criteria = implode("1 AND ", $crit);
        } else {
            $criteria = "";
        }

        App::Import("Model", "ServiceUnit");
        $this->ServiceUnit = new ServiceUnit();

        $unitMainData = $this->paginate('ServiceUnit', array($criteria));

        $this->set('unitMainData', $unitMainData);

        if ($this->request->is('ajax')) {
            $this->viewPath = 'elements' . DS . 'serviceunits';
            $this->render('unitmaintenance', 'ajax');
        }//if
    }
    
    /**
    * @Method       :add
    * @Description  :to add Service Units
    * @access       :public
    * @param        :null
    * @return       :null
    */
    public function add() {
        $userId = $this->Auth->User("id");
        $userType = $this->Auth->User('user_type');

        if ($this->request->is('post')) {
            $this->ServiceUnit->create();
            if ($this->ServiceUnit->save($this->request->data)) {
                $msg = __("SERVICE_UNIT_ADD_SUCCESS", true);
                $this->Session->setFlash('<div class="totalSuccess">' . $msg . '</div>');
                return $this->redirect(array('action' => 'index'));
            }
            $msg = __("SERVICE_UNIT_ADD_FAILURE", true);
            $this->Session->setFlash('<div class="totalErrors">' . $msg . '</div>');
        }
        $this->set('serviceGroups', $this->getServiceGroupsList());
    }
    
    /**
    * @Method       :edit
    * @Description  :to edit Service Units
    * @access       :public
    * @param        :null
    * @return       :null
    */
    public function edit($id = null) {
        $userId   = $this->Auth->User("id");
	$userType = $this->Auth->User('user_type');
        
        
        if (!$id) {
            throw new NotFoundException(__('INVALID_SERVICE_UNIT'));
        }
        $serviceUnit = $this->ServiceUnit->findById($id);
        if (!$serviceUnit) {
            throw new NotFoundException(__('INVALID_SERVICE_UNIT'));
        }
        if ($this->request->is(array('ServiceUnit', 'put'))) {
            $this->ServiceUnit->id = $id;
            if ($this->ServiceUnit->save($this->request->data)) {
                $msg = __("SERVICE_UNIT_UPDATE_SUCCESS", true);
                $this->Session->setFlash('<div class="totalSuccess">' . $msg . '</div>');
                return $this->redirect(array('action' => 'index'));
            }
            $msg = __("SERVICE_UNIT_UPDATE_FAILURE", true);
            $this->Session->setFlash('<div class="totalErrors">' . $msg . '</div>');
        }
        if (!$this->request->data) {
            $this->request->data = $serviceUnit;
        }
        $this->set('serviceGroups', $this->getServiceGroupsList());
    }
    
    /**
    * @Method       :getServiceGroupsList
    * @Description  :to get a list of Service Groups
    * @access       :public
    * @param        :null
    * @return       :null
    */
    public function getServiceGroupsList(){
        Controller::loadModel('ServiceGroup');
        return $this->ServiceGroup->find('list', array('fields' => array('ServiceGroup.id', 'ServiceGroup.group_name')));
    }
}